package com.zb.designmode;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {

//        testHashcodeConflict();
//        printHashcode();
//        testMapResize();
//        testCopyOnWriteArrayList();
    }

    private static void testHashcodeConflict() {
        Map<String, String> map = new HashMap<>();
        map.put("重地", "111");
        System.out.println(map.size());
        map.put("通话", "222");
        System.out.println(map.size());
        map.put("方面", "333");
        System.out.println(map.size());
        map.put("树人", "333");
        System.out.println(map.size());

    }

    private static void printHashcode() {
        System.out.println(new String("重地").hashCode());
        System.out.println(new String("通话").hashCode());
        System.out.println(new String("方面").hashCode());
        System.out.println(new String("树人").hashCode());
    }

    private static void testMapResize() {
        Map<Integer, Integer> map = new HashMap();
        for (int i = 0; i < 13; i++) {
            if (map.size() >= 12) {
                System.out.println("resize");
            }
            map.put(i, i);
        }

    }

    private static void testMainThreadAndChildThreadPrority() {
        Thread thread = new Thread(() -> {
            System.out.print("world");
        });
        thread.start();
        System.out.print("hello");
    }

    private static void testSet() {
        String a = "abc";
        String b = new String("abc");

        Set<String> set = new HashSet();
        set.add(a);
        set.add(b);

        System.out.println(set.size());
    }

    private static void testCopyOnWriteArraySet() {
        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet();
        set.add("111");
        set.add("333");
        set.add("222");

        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    private static void testCopyOnWriteArrayList() {
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        list.add("one");
        list.add("three");
        list.add("two");
        System.out.println(list.get(1));
        if (list.contains("three")) {
            Iterator<String> value = list.iterator();
            while (value.hasNext()) {
                System.out.println(value.next());
            }
        }

        CopyOnWriteArrayList<String> listClone = (CopyOnWriteArrayList<String>) list.clone();
        list.add("four");

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        Iterator<String> itClone = listClone.iterator();
        while (itClone.hasNext()) {
            System.out.println(itClone.next());
        }
    }
}
